<template>
  <div>
     <!-- 主体部分 -->
    <section class="main">
      <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isSon"/>
      <label for="toggle-all">Mark all as complete</label>
      <ul class="todo-list">
        <!-- 当任务已完成，可以给 li 加上 completed 类，会让元素加上删除线 -->
        <li :class="{completed:item.isDone}" v-for="item in showList" :key="item.id">
          <div class="view">
            <input class="toggle" type="checkbox" :checked="item.isDone" @change="myChange(item.id)"/>
            <label>{{item.name}}</label>
            <button @click="del(item.id)" class="destroy"></button>
          </div>
        </li>
      </ul>
    </section>

  </div>
</template>

<script>
export default {
  props:{
    list:{
        type: Array,
        required:true
    },
    type:String
  },
  data () {
    return {

    }
  },
  computed:{
    showList(){
      if(this.type == 'active' ){
        // 进行中
        return this.list.filter(item=>!item.isDone)
      }else if(this.type == 'completed'){
        // 已完成
        return this.list.filter(item=>item.isDone)
      }
      return this.list

    },
    isSon:{
      get () {
        return this.list.every(item=>item.isDone)
      },
      set (value) {
        this.$emit('isAll', value)
      }

    }
  },
  created () {

  },

  methods: {
    // 删除
    del(id){
      this.$emit('del',id)
    },
    // 修改状态
    myChange(id){
      this.$emit('myChange',id)
    }
  }
}

</script>

<style scoped lang='scss'>

</style>
